package com.mairuide.bill.utils;

import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

public class DateUtils {

    public static Date getDate(String st){
        Date date = null;
        if (st==null)return null;
        String pattern= "yyyy/MM/dd HH:mm:ss";
        String pattern2= "yyyy-MM-dd HH:mm:ss";
        try {
            if (st.trim().length()<11){
                pattern= "yyyy/MM/dd";
                pattern2= "yyyy-MM-dd";
            }
            if (st.length()>19) {
            	st = st.substring(0, 19);
            }
            if (Pattern.matches("\\d{4}年\\d{1,2}月\\d{1,2}日 \\d{2}:\\d{2}:\\d{2}", st)){
                SimpleDateFormat sd = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                date = sd.parse(st);
            }else if (Pattern.matches("\\d{4}/\\d{2}/\\d{2}", st)){
                SimpleDateFormat sd = new SimpleDateFormat(pattern);
                date = sd.parse(st);
            }else if (Pattern.matches("\\d{4}/\\d{1,2}/\\d{1,2} \\d{1,2}:\\d{2}:\\d{2}", st)){
                SimpleDateFormat sd = new SimpleDateFormat(pattern);
                date = sd.parse(st);
            }else if (Pattern.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}\\.\\d{2}\\.\\d{2}", st)){
                SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
                date = sd.parse(st);
            }else if (Pattern.matches("\\d{4}-\\d{2}-\\d{2}", st)){
                SimpleDateFormat sd = new SimpleDateFormat(pattern2);
                date = sd.parse(st);
            }else if (Pattern.matches("\\d{4}\\d{2}\\d{2} \\d{4}\\.\\d{2}\\.\\d{2}", st)){
                SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd HH.mm.ss");
                date = sd.parse(st);
            }else if (Pattern.matches("\\d{4}\\d{2}\\d{2}", st)){
                SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd");
                date = sd.parse(st);
            }else if (Pattern.matches("\\d{4}\\d{2}\\d{2} \\d{2}:\\d{2}:\\d{2}", st)){
                SimpleDateFormat sd = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
                date = sd.parse(st);
            }else if (Pattern.matches("\\d{4}\\d{2}\\d{2} \\d{2}\\d{2}\\d{2}", st)||Pattern.matches("\\d{4}\\d{2}\\d{2}\\d{2}\\d{2}\\d{2}", st)){
                SimpleDateFormat sd = st.contains(" ")?new SimpleDateFormat("yyyyMMdd HHmmss"):new SimpleDateFormat("yyyyMMddHHmmss");
                date = sd.parse(st);
            }else if (Pattern.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}", st)){
                SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                date = sd.parse(st);
            }else if (Pattern.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}\\d{2}\\d{2}", st)){
                SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HHmmss");
                date = sd.parse(st);
            }else if (Pattern.matches("\\d{4}/\\d{2}/\\d{2}", st)){
                SimpleDateFormat sd = new SimpleDateFormat("yyyy/MM/dd");
                date = sd.parse(st);
            }else if (Pattern.matches("\\d{1,2}/\\d{1,2}/\\d{2} \\d{1,2}:\\d{1,2}:\\d{1,2}", st)){
                SimpleDateFormat sd = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
                date = sd.parse(st);
            }else if (Pattern.matches("\\d{6}", st)){
                SimpleDateFormat sd = new SimpleDateFormat("HH:mm:ss");
                date = sd.parse(st);
            }else if (Pattern.matches("\\d{14}", st)){
                SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                date = sd.parse(st);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return date;
        }

    }

    public static String getDateString (Date date){
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sd.format(date);
        return format;
    }

    /**
     *
     * @author 刘中华
     * @date 2018/11/22 16:35
     * @param [str]
     * @return java.lang.String
     * @Description 获取格式化后的日期字符串 目前只支持20060704093715 20060704 093715
     */
    public static String getFormatDateString(String str){
        if (StringUtils.isBlank(str)){
            return "";
        }
        Pattern pattern = Pattern.compile("\\d+");
        if (pattern.matcher(str).matches()){
            if (str.length()==8){ //说明是20060704 类型  只有年月日
                String year = str.substring(0, 4);
                String month = str.substring(4, 6);
                String day = str.substring(6, 8);
                return year+"-"+month+"-"+day;
            }else if (str.length()==6){ //说明是093715   类型  只有时分秒
                String hour = str.substring(0, 2);
                String minute = str.substring(2, 4);
                String second = str.substring(4, 6);
                return hour+"-"+minute+"-"+second;
            }else if (str.length()==14){  //说明是20060704093715   类型  年月日时分秒
                String year = str.substring(0, 4);
                String month = str.substring(4, 6);
                String day = str.substring(6, 8);
                String hour = str.substring(8, 10);
                String minute = str.substring(10, 12);
                String second = str.substring(12,14);
                return year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
            }
        }
        return "";
    }

    public static String formatTimeString(String s,String format){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf2 = new SimpleDateFormat(format);
            Date date = sdf.parse(s);
            return sdf2.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 格式化字符串20060704093715这类型的  不够补0
     * @param s
     * @return
     */
    public static String getTimestampString(String s){
        String pattern = "\\d+";
        boolean isMatch = Pattern.matches(pattern, s);
        if (!isMatch) return null;
        if (s==null) return null;
        if (s.length()<=14){
            int i=14- s.length();
            String str="";
            for (int j=0;j<i;j++){
                str+="0";
            }
            return s+str;
        }
        return null;
    }

    /**
     * @Author 刘中华
     * @Date 2019/1/16 16:34
     * @Param [year]
     * @return java.util.List<java.lang.String>
     * @Description 输入年份 得到月份的集合
     **/
    public static List<String> getYearMonth(List<String> year){
        ArrayList<String> yearList = new ArrayList<>();
        for (int k = 0; k < year.size();k++) {
            for (int i = 1; i < 13; i++) {
                String month="";
                if (i<10){
                    month=year.get(k)+"-0"+i;
                }else{
                    month=year.get(k)+"-"+i;
                }
                yearList.add(month);
            }
        }
        return yearList;
    }
    /**
     * @Author 刘中华
     * @Date 2019/6/12 11:37
     * @Param [s]
     * @return 字符串  yyyy-MM-dd   转Timestamp类型
     * @Description
     **/
    public static Timestamp stringToTimestamp(String s){
        try {
            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = format1.parse(s);
            return Timestamp.valueOf(format2.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("<<<<<<<<<<<<<<<<<字符串  yyyy-MM-dd   转Timestamp类型 失败");
        }
        return null;
    }


    public static void main(String[] arg){
        System.out.println(getFormatDateString("20060704"));
        System.out.println(getFormatDateString("093715"));
        System.out.println(getFormatDateString("20060704093715"));
        System.out.println(getDate("2010-05-04 "));
        ArrayList<String> objects = new ArrayList<>();
        objects.add("2016");
        objects.add("2017");

        System.out.println(Pattern.matches("\\d{4}/\\d{2}/\\d{2}","2012/12/11"));
        System.out.println(Pattern.matches("\\d{4}年\\d{2}月\\d{2}日 \\d{2}:\\d{2}:\\d{2}", "2010年12月11日 21:22:22"));
        System.out.println(Pattern.matches("\\d{4}-\\d{1,2}-\\d{1,2} \\d{1,2}\\.\\d{1,2}\\.\\d{1,2}", "2010-12-11 21.22.22"));

        try {
            String st="3/7/19 14:33:47";
            SimpleDateFormat sd = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
            Date date = sd.parse(st);
            System.out.println(date);

            System.out.println(Pattern.matches("\\d{1,2}/\\d{1,2}/\\d{2} \\d{1,2}:\\d{1,2}:\\d{1,2}", st));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }


}
